package com.zly.distributed.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class ReEntryLockDemo {
    final Object object = new Object();
    ReentrantLock lock = new ReentrantLock();

    public void demo1() {
        new Thread(() -> {
            lock.lock();
            synchronized (object) {
                lock.lock();
                System.out.println("object = 外层调用");
                synchronized (object) {
                    System.out.println("object = 中层调用");
                }
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (Exception e) {
                    System.out.println("e = " + e);
                }

                lock.unlock();
            }
            lock.unlock();
        }, "t1").start();
        try {
            TimeUnit.SECONDS.sleep(2);
            new Thread(() -> {
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName() + "开始运行");
                lock.lock();
                System.out.println("object = " + object);
                lock.unlock();
            }, "t1").start();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        ReEntryLockDemo reEntryLockDemo = new ReEntryLockDemo();
        reEntryLockDemo.demo1();
    }
}
